package com.etcn.modules.statical.base.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.etcn.common.annotation.QueryCache;
import com.etcn.common.service.impl.CrudServiceImpl;
import com.etcn.common.utils.ChineseCharToEn;
import com.etcn.modules.statical.base.dao.IBaseCountryDao;
import com.etcn.modules.statical.base.pojo.BaseCountry;
import com.etcn.modules.statical.base.service.IBaseCountryService;

/**
 * 国家serviceImpl
 * 
 * @author guozc
 * @date 2018-01-12 09:26:26
 */
@Service("baseCountryService")
public class BaseCountryServiceImpl extends CrudServiceImpl<IBaseCountryDao, BaseCountry>
		implements IBaseCountryService {
	
	@QueryCache
	@Override
	public List<BaseCountry> findList(BaseCountry entity) throws Exception {
		List<BaseCountry> countrys = super.findList(entity);
		if (countrys != null) {
			for (BaseCountry country : countrys) {
				if(country.getImportExponent() == null){
					country.setImportExponent("");
				}
				if(country.getExportExponent() == null){
					country.setExportExponent("");
				}
			}
		}
		return countrys;
	}

	@QueryCache
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> findCountrysDir(BaseCountry entity) throws Exception {
		List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
		List<BaseCountry> countrys = super.findList(entity);
		Map<String, List<BaseCountry>> indexMap = new HashMap<String, List<BaseCountry>>();
		if (countrys != null) {
			for (BaseCountry country : countrys) {
				// 取得首字符
				String firstLetter = ChineseCharToEn.getFirstLetter(country.getName()).toUpperCase();
				List<BaseCountry> indexList = indexMap.get(firstLetter);
				if (indexList == null) {
					indexList = new ArrayList<BaseCountry>();
					indexMap.put(firstLetter, indexList);
				}
				indexList.add(country);
			}
			for (Map.Entry<String, List<BaseCountry>> entry : indexMap.entrySet()) {
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("thumname", entry.getKey());
				m.put("countrylist", entry.getValue());
				datas.add(m);
			}
			Collections.sort(datas, new Comparator<Object>() {
				@Override
				public int compare(Object o1, Object o2) {
					Map<String, Object> o1m = (Map<String, Object>)o1;
					Map<String, Object> o2m = (Map<String, Object>)o2;
					return ((String)o1m.get("thumname")).compareTo((String)o2m.get("thumname"));
				}
			});
		}
		return datas;
	}

	@Override
	public Map<String, Object> detail(long id) throws Exception {
		Map<String,Object> retMap = new HashMap<String,Object>();
		// 商品信息
		BaseCountry country = getById(id);
		retMap.put("country", country);
		return retMap;
	}

	@Override
	public BaseCountry getByCode(String code) throws Exception {
		return dao.getByCode(code);
	}

	@Override
	public List<BaseCountry> findSimpleList() throws Exception {
		return dao.findSimpleList();
	}

}